package bdz.projects.cbt.site
{
	import bdz.common.events.DispatchChannel;
	import bdz.common.events.XMLLoaderEvent;
	import bdz.common.model.AppEnvironment;
	import bdz.common.model.XMLLoader;
	import bdz.projects.cbt.CBTGame;
	import bdz.projects.cbt.core.CBTPlayer;
	import bdz.projects.cbt.core.CBTProducts;
	import bdz.projects.cbt.events.CBTGameEvent;
	import bdz.projects.cbt.events.CBTUIEvent;
	import bdz.projects.cbt.ui.CBTInventoryList;
	import bdz.projects.cbt.ui.CBTPanelLoader;
	import bdz.projects.cbt.ui.CBTProductList;
	
	import flash.display.MovieClip;
	import flash.display.StageQuality;
	import flash.events.Event;
	
	import gs.*;
	import gs.easing.*;
	public dynamic class CBTSite extends MovieClip {
		
		private var _game:CBTGame;
		private var _products:CBTProducts;
		private var _config:Object;
		private var _env:AppEnvironment = AppEnvironment.getEnv();
		private var _eventChannel:DispatchChannel = DispatchChannel.getChannel();
		
		private var _transactionsFrame:MovieClip;
		private var _basicControls:MovieClip;
		private var _productTable:CBTProductList;
		private var _inventoryTable:CBTInventoryList;
		private var _transactionControls:MovieClip;
		private var _playerFunctions:MovieClip;
		private var _dayCounter:MovieClip;
		private var _locationIndicator:MovieClip;
		
		public var _startForm:MovieClip;
		private var _panelLoader:CBTPanelLoader;
		private var _inventoryNotification:MovieClip;
		private var _mortgageNotification:MovieClip;
		public function CBTSite(){
			super();
			addEventListener(Event.ADDED_TO_STAGE, initializeSite);
		}
		private function applyUIReference():void{
			
			_inventoryNotification = this["inventoryNotification"];
			_mortgageNotification = this["mortgageNotification"];
			
			_basicControls = this["basicControls"];
			_productTable = this["productTable"] as CBTProductList;
			_inventoryTable = this["inventoryTable"] as CBTInventoryList;
			
			_transactionsFrame = this["transactionsFrame"];
			_dayCounter = this["dayCounter"];
			_playerFunctions = this["playerFunctions"];
			_transactionControls = this["transactionControls"];
			_locationIndicator = this["locationIndicator"];
			_panelLoader = this["panelLoader"];
			_transactionsFrame.visible = false;
			_locationIndicator.visible = false;
			_productTable.visible = false;
			_inventoryTable.visible = false;
			_dayCounter.visible = false;
			_transactionControls.visible = false;
			_playerFunctions.visible = false;
			_basicControls.visible = false;
		}
		private function listenToGameEvents():void{
			
		}
		private function listenToUIEvents():void{
			_eventChannel.addEventListener(CBTUIEvent.START_GAME, onStartGame);
			_eventChannel.addEventListener(CBTGameEvent.GAME_RESET, resetGame);
			_eventChannel.addEventListener(CBTUIEvent.NOTIFY_INVENTORY, checkNotification);
			_eventChannel.addEventListener(CBTUIEvent.NOTIFY_MORTGAGE, checkNotification);
			
		}
		private function hideNotification(targ:MovieClip):void{
			TweenMax.to(targ, 1, {delay:4, alpha:0, ease:Strong.easeInOut});
		}
		private function checkNotification(evt:CBTUIEvent):void{
			if(evt.type == CBTUIEvent.NOTIFY_INVENTORY){
				TweenMax.to(_inventoryNotification, 1, {alpha:1, ease:Strong.easeInOut, onComplete:hideNotification, onCompleteParams:[_inventoryNotification]});
			}else{
				TweenMax.to(_mortgageNotification, 1, {alpha:1, ease:Strong.easeInOut, onComplete:hideNotification, onCompleteParams:[_mortgageNotification]});
			}
		}
		private function showGameUI():void{
			var _ease:Function = Strong.easeInOut;
			
			_transactionControls.visible = true;
			_transactionControls.alpha = 0;
			
			_playerFunctions.visible = true;
			_playerFunctions.alpha = 0;
			
			_transactionsFrame.visible = true;
			_transactionsFrame.alpha = 0;
			_transactionsFrame.scaleX = _transactionsFrame.scaleY = .6;
			
			_dayCounter.alpha = 0;
			_dayCounter.visible = true;
			
			
			_productTable.alpha = 0;
			_productTable.visible = true;
			
			_inventoryTable.alpha = 0;
			_inventoryTable.visible = true;
			
			_locationIndicator.alpha = 0;
			_locationIndicator.visible = true;
			
			
			TweenMax.to(_transactionsFrame, .8, {alpha:1, scaleX:1, scaleY:1, ease:_ease});
			var delay:Number = .5;
			
			TweenMax.to(_productTable, .8, {delay:delay, alpha:1, ease:_ease});
			TweenMax.to(_inventoryTable, .8, {delay:delay+.2, alpha:1, ease:_ease});
			TweenMax.to(_transactionControls, .8, {delay:delay+.2, alpha:1, ease:_ease});
			TweenMax.to(_dayCounter, .8, {delay:delay+.2, alpha:1, ease:_ease});
			TweenMax.to(_playerFunctions, .8, {delay:delay+.2, alpha:1, ease:_ease});
			TweenMax.to(_locationIndicator, .8, {delay:delay+.2, alpha:1, ease:_ease});
			
			
			
		}
		private function onStartGame(evt:CBTUIEvent):void{
			var player:CBTPlayer = new CBTPlayer();
			player.email = evt.data.email;
			player.realName = evt.data.realname;
			player.playerName = evt.data.playername;
			player.startingCash = _env.getSetting("startCash");
			player.startingMortgage = _env.getSetting("startMortgage");
			player.startingPaypal = 0;
			player.maxDays = evt.data.numDays;
			_game = CBTGame.getInstance();
			_game.startGame(evt.data.numDays, player, _products, evt.data.location);
			_game.mortgageRate = _env.getSetting("mortgageInterestRate");
			_game.paypalRate = _env.getSetting("paypalRate");
			_game.inventoryList = _inventoryTable;
			_game.productList = _productTable; 
			
			_panelLoader.game = _game;
			_panelLoader.productList = _productTable;
			_panelLoader.inventoryList = _inventoryTable;
			
			showGameUI();
			
		}
		private function resetGame(evt:CBTGameEvent):void{
			_transactionsFrame.visible = false;
			_locationIndicator.visible = false;
			_productTable.visible = false;
			_inventoryTable.visible = false;
			_dayCounter.visible = false;
			_transactionControls.visible = false;
			_playerFunctions.visible = false;
			_inventoryTable.fullClear();
			_productTable.fullClear();
			_game.finished = false;
			
			_startForm = new StartForm();
			_startForm.alpha = 0;
			_startForm.x = 511;
			_startForm.y = 312;
			addChild(_startForm);
			
		}
		private function initializeSite(evt:Event):void{
			stage.quality = StageQuality.BEST;
			applyUIReference();
			listenToUIEvents();
			
			var xm:XMLLoader = new XMLLoader("configLoaded");
			xm.addEventListener("configLoaded", onConfigurationLoaded);
			xm.location = "data/config.xml";
			xm.send();
			
		}
		
		private function onProductItemSelect(evt:CBTUIEvent):void{
			_transactionControls.disableSellButton();
			_transactionControls.enableBuyButton();
			_inventoryTable.removeSelection();
			
		}
		private function onInventoryItemSelect(evt:CBTUIEvent):void{
			_transactionControls.disableBuyButton();
			_transactionControls.enableSellButton();
			_productTable.removeSelection();
		}
		public function showStartForm():void{
			TweenMax.to(this["startpage"], 1, {alpha:0, onComplete:removeChild, onCompleteParams:[this["startpage"]], ease:Strong.easeInOut});
			_startForm = new StartForm();
			_startForm.alpha = 0;
			_startForm.x = 511;
			_startForm.y = 312;
			addChild(_startForm);
		}
		private function onConfigurationLoaded(evt:XMLLoaderEvent):void{
			var xm:XML = new XML(evt.data);
			_env.addSetting("base", this);
			if(_env.getSetting("baseURL") == undefined){
				_env.addSetting("baseURL", loaderInfo.parameters.baseURL || "http://www.paypal-education.com.au/sell/scripts/");
			}
			_env.addSetting("cheapExpensiveChance", Number(xm.cheap_expensive_chance.@value));
			_env.addSetting("stealRate", Number(xm.steal_rate.@value));
			_env.addSetting("travelFare", Number(xm.travel_fare.@value));
			_env.addSetting("startCash", Number(xm.start_cash.@value));
			_env.addSetting("startMortgage", Number(xm.start_mortgage.@value));
			_env.addSetting("paypalRate", Number(xm.paypal_rate.@value));
			_env.addSetting("mortgageInterestRate", Number(xm.mortgage_interest_rate.@value));
			_env.addSetting("startCapacity", Number(xm.starting_capacity.@value));
			
			
			var blist:XMLList = xm.buymessages.message;
			var slist:XMLList = xm.sellmessages.message;
			
			var sellMessages:Array = new Array();
			for each(var sxm:XML in slist){
				
				sellMessages.push(sxm.@value);
			}
			var buyMessages:Array = new Array();
			for each(var bxm:XML in blist){
				
				buyMessages.push(bxm.@value);
			}
			
			_env.addSetting("sellMessages", sellMessages);
			_env.addSetting("buyMessages", buyMessages);
			
			_products = CBTProducts.getInstance(xm.products.product);
			
			_basicControls.alpha = 0;
			_basicControls.visible = true;
			TweenMax.to(_basicControls, 1, {alpha:1, ease:Strong.easeInOut});
			
			
			
			_eventChannel.addEventListener(CBTUIEvent.PRODUCT_ITEM_SELECTED, onProductItemSelect);
			_eventChannel.addEventListener(CBTUIEvent.INVENTORY_ITEM_SELECTED, onInventoryItemSelect);
			
		}
		
	}
}